package com.duoduo.algs4.util;

import edu.princeton.cs.introcs.Draw;
import edu.princeton.cs.introcs.StdOut;
import edu.princeton.cs.introcs.StdRandom;

public class VisualAccumulator extends Accumulator{
	Draw draw ;
	//id累加器名字，trials尝试次数，max最大值
	public VisualAccumulator(String id,int trials,double max){
		this.id = id;
		draw = new Draw(id);
		draw.setXscale(0,trials);
		draw.setYscale(0,max);
		draw.setPenRadius(.005);
	}
	
	public void addDataValue(double val) {
		N++;
		total += val;
		draw.setPenColor(Draw.DARK_GRAY);
		draw.point(N, val);
		draw.setPenColor(Draw.RED);
		draw.point(N, mean());
	}

	public static void main(String[] args) {
		int N = 10000;
		VisualAccumulator head = new VisualAccumulator("随机数测试",N,1.0);
		for (int i = 0; i < N; i++) {
			head.addDataValue(StdRandom.random());
		}
		StdOut.println(head);
		assert head.mean() > 4 && head.mean() < 5 ;

	}

}
